﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PieChart.cs" company="">
//   
// </copyright>
// <summary>
//   The pie chart.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;

namespace Graphix.Charts
{
    using Graphix.Charts.Base;
    using Graphix.Charts.Values;
    using Graphix.Elements;

    /// <summary>
    /// The pie chart.
    /// </summary>
    public class PieChart : BothAxisAndLegend<Simple>
    {
        #region Fields

        /// <summary>
        /// The keys.
        /// </summary>
        private readonly List<string> keys = new List<string>();

        private float spaceBetweenElements;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the display mode.
        /// </summary>
        public DisplayMode DisplayMode { get; set; }

        #endregion

        public PieChart()
        {
            this.values = new List<ValueSet<Simple>>();
            this.SpaceBetweenElements = 0.5f;
        }

        #region Public Methods and Operators

        /// <summary>
        /// The add data.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        public void AddData(string name, float value)
        {
            var newelement = new List<Simple> { new Simple { Value = value } };
            this.keys.Add(name);
            this.values.Add(new ValueSet<Simple>(this.GetNextColor(), name, newelement));
        }

        #endregion

        #region Methods

        /// <summary>
        /// The draw chart.
        /// </summary>
        protected override void DrawChart()
        {
            this.PreDraw();

            if (this.keys.Count > 0)
            {
                float sum = this.values.Sum(x => x.Values.Sum(y => y.Value));
                float sumDegree = 0;

                this.DrawTitle();
                if (this.DisplayLegend)
                {
                    this.BuildLegend(this.keys, this.values);
                }

                float size = this.Border.Width;
                if (this.Border.Height < size)
                {
                    size = this.Border.Height;
                }

                float left = this.Border.Left;
                float top = this.Border.Top;

                for (int i = 0; i < this.values.Count; i++)
                {
                    ValueSet<Simple> one = this.values[i];
                    float value = one.Values.First().Value;
                    float next = value / sum * 360;
                    var element = new PiePart(left, top, size, sumDegree, next,
                        new SolidBrush(one.Color))
                        {
                            BorderColor = this.Background.Color
                        };
                    element.Attributes["g"] = i.ToString(CultureInfo.InvariantCulture);
                    element.Attributes["data-ot"] = string.Format("l={0};v=[{1}]", one.Name, value);
                    this.AddElement(element);

                    sumDegree += next;
                }
            }
        }

        protected override string JavascriptLabelsValues()
        {
            return "legend";
        }

        public float SpaceBetweenElements
        {
            get
            {
                return this.spaceBetweenElements;
            }

            set
            {
                if (value < 0)
                {
                    this.spaceBetweenElements = 0;
                }
                else if (value > 1)
                {
                    this.spaceBetweenElements = 1;
                }
                else
                {
                    this.spaceBetweenElements = value;
                }
            }
        }

        #endregion
    }
}